//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.eova.widget;

import com.alibaba.fastjson.JSONObject;
import com.eova.common.utils.xx;
import com.eova.model.Menu;
import com.eova.model.MetaField;
import com.eova.widget.tree.TreeNode;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class WidgetUtil {
    public WidgetUtil() {
    }

    public static void copyValueColumn(List<Record> reList, String pkName, List<MetaField> eis) {
        Iterator var4 = reList.iterator();

        while(var4.hasNext()) {
            Record re = (Record)var4.next();
            Iterator var6 = eis.iterator();

            while(var6.hasNext()) {
                MetaField x = (MetaField)var6.next();
                String exp = x.getStr("exp");
                if(!xx.isEmpty(exp)) {
                    String en = x.getEn();
                    re.set(en + "_val", re.get(en, "").toString());
                }
            }

            re.set("pk_val", re.get(pkName).toString());
        }

    }

    public static String menu2TreeJson(Map<Integer, Menu> temp, Integer rootId) {
        LinkedHashMap recordMap = new LinkedHashMap();
        Iterator var4 = temp.entrySet().iterator();

        while(var4.hasNext()) {
            Entry map = (Entry)var4.next();
            recordMap.put((Integer)map.getKey(), (new Record()).setColumns((Model)map.getValue()));
        }

        return map2TreeJson(recordMap, rootId);
    }

    public static String map2TreeJson(Map<Integer, Record> temp, Integer rootId) {
        Iterator sb = temp.entrySet().iterator();

        String json;
        while(sb.hasNext()) {
            Entry pid = (Entry)sb.next();
            Record childList = (Record)pid.getValue();
            json = "open";
            Boolean pr = childList.getBoolean("is_collapse");
            if(!xx.isEmpty(pr) && pr.booleanValue()) {
                json = "closed";
            }

            childList.set("state", json);
            childList.set("iconCls", childList.getStr("icon"));
            childList.remove("is_collapse");
            childList.remove("icon");
            childList.remove("url");
            childList.remove("diy_js");
            childList.remove("config");
            childList.remove("biz_intercept");
        }

        temp.put(rootId, new TreeNode());
        Iterator childList2 = temp.entrySet().iterator();

        Record json1;
        while(childList2.hasNext()) {
            Entry sb1 = (Entry)childList2.next();
            if(((Integer)sb1.getKey()).intValue() != 0) {
                json1 = (Record)sb1.getValue();
                Integer pid1 = json1.getInt("parent_id");
                Record pr1 = (Record)temp.get(pid1);
                if(pr1 != null) {
                    Object childList1 = (List)pr1.get("children");
                    if(childList1 == null) {
                        childList1 = new ArrayList();
                        ((Record)temp.get(pid1)).set("children", childList1);
                    }

                    ((List)childList1).add(json1);
                }
            }
        }

        StringBuilder sb2 = new StringBuilder("[");
        List childList3 = (List)((Record)temp.get(rootId)).get("children");
        Iterator pr2 = childList3.iterator();

        while(pr2.hasNext()) {
            json1 = (Record)pr2.next();
            sb2.append(JsonKit.toJson(json1));
            sb2.append(",");
        }

        sb2.delete(sb2.length() - 1, sb2.length());
        sb2.append("]");
        json = sb2.toString();
        json = json.replaceAll("iconcls", "iconCls");
        return json;
    }

    public static void formatEasyTree(Map<Integer, Menu> maps) {
        Iterator var2 = maps.entrySet().iterator();

        while(var2.hasNext()) {
            Entry map = (Entry)var2.next();
            Menu x = (Menu)map.getValue();
            JSONObject attrs = new JSONObject();
            if(!x.getStr("type").equals("dir")) {
                attrs.put("url", x.getUrl());
            }

            x.put("id", x.getInt("id"));
            x.put("text", x.getStr("name"));
            x.put("attributes", attrs);
            x.remove("name");
        }

    }

    public static void getParent(Map<Integer, Menu> allMenu, Map<Integer, Menu> authParent, Menu menu) {
        Integer pid = menu.getInt("parent_id");
        if(!xx.isEmpty(pid) && pid.intValue() != 0) {
            Menu pm = (Menu)allMenu.get(pid);
            authParent.put(pid, pm);
            getParent(allMenu, authParent, pm);
        }
    }

    public static LinkedHashMap<Integer, Menu> menusToLinkedMap(List<Menu> records) {
        LinkedHashMap temp = new LinkedHashMap();
        Iterator var3 = records.iterator();

        while(var3.hasNext()) {
            Menu x = (Menu)var3.next();
            String icon = (String)x.get("icon");
            if(xx.isEmpty(icon)) {
                icon = "icon-application";
            }

            x.set("icon", icon);
            temp.put(x.getInt("id"), x);
        }

        return temp;
    }

    public static Map<Integer, Record> listToLinkedMap(List<Record> records) {
        LinkedHashMap temp = new LinkedHashMap();
        Iterator var3 = records.iterator();

        while(var3.hasNext()) {
            Record x = (Record)var3.next();
            String icon = (String)x.get("icon");
            if(xx.isEmpty(icon)) {
                icon = "icon-application";
            }

            x.set("icon", icon);
            temp.put(x.getInt("id"), x);
        }

        return temp;
    }
}
